import sys
from PySide6.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout,
    QLabel, QPushButton, QListWidget, QListWidgetItem, QCheckBox, QComboBox,
    QGroupBox, QFrame, QSizePolicy
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont


class FileProcessingUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("批量文件处理")
        self.resize(1200, 800)

        # 设置深色主题
        self.set_dark_theme()

        # 创建主窗口部件
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        # 主布局
        self.main_layout = QVBoxLayout(self.central_widget)
        self.main_layout.setContentsMargins(10, 10, 10, 10)
        # self.main_layout.setSpacing(0)

        # 创建顶部栏
        self.create_top_bar()

        # 创建主体部分
        self.create_main_body()

        # 创建底部按钮
        self.create_bottom_buttons()

    def set_dark_theme(self):
        # 设置深色主题样式
        self.setStyleSheet("""
            QMainWindow, QWidget {
                background-color: #2D2D2D;
                color: #E0E0E0;
            }
            QLabel, QCheckBox, QPushButton {
                color: #E0E0E0;
            }
            QGroupBox {
                border: 1px solid #3D3D3D;
                border-radius: 5px;
                margin-top: 20px;
                padding-top: 10px;
                font-weight: bold;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                top: -8px;
                padding: 0 3px;
            }
            QPushButton {
                background-color: #3D3D3D;
                border: 1px solid #555555;
                border-radius: 4px;
                padding: 6px 12px;
            }
            QPushButton:hover {
                background-color: #4A4A4A;
            }
            QPushButton:disabled {
                color: #888888;
            }
            QListWidget {
                background-color: #252525;
                border: 1px solid #444444;
                border-radius: 4px;
            }
            QFrame {
                background-color: #252525;
                border: 1px solid #444444;
                border-radius: 4px;
            }
            QCheckBox::indicator {
                width: 18px;
                height: 18px;
            }
        """)

        # 设置VIP按钮的样式
        self.setStyleSheet(self.styleSheet() + """
            QPushButton.vip-button {
                background-color: #D69521;
                color: #FFFFFF;
                font-weight: bold;
            }
            QPushButton.vip-button:hover {
                background-color: #E8A52D;
            }
        """)

        # 设置开始输出按钮的样式
        self.setStyleSheet(self.styleSheet() + """
            QPushButton.start-button {
                background-color: #2A6EDF;
                color: #FFFFFF;
                width: 80px;
                font-weight: bold;
                padding: 6px 20px;
            }
            QPushButton.start-button:hover {
                background-color: #3480FA;
            }
        """)

        self.setStyleSheet(self.styleSheet() + """
            QPushButton.close-button {
                background-color: #383e4a;
                color: #FFFFFF;
                width: 80px;
                font-weight: bold;
                padding: 6px 20px;
            }
            QPushButton.close-button:hover {
                background-color: #383e4a;
            }
        """)

    def create_top_bar(self):
        # 顶部布局
        top_layout = QHBoxLayout()
        top_layout.setSpacing(15)

        # 标题
        title_label = QLabel("批量输出为图片")
        title_font = QFont()
        title_font.setPointSize(16)
        title_font.setBold(True)
        title_label.setFont(title_font)

        top_layout.addWidget(title_label)
        top_layout.addStretch(1)

        self.main_layout.addLayout(top_layout)

    def create_main_body(self):
        # 主水平布局（左、中、右）
        body_layout = QHBoxLayout()
        body_layout.setSpacing(15)

        # 左侧文件列表区域
        file_list_widget = self.create_file_list_widget()

        # 中间预览区域
        preview_widget = self.create_preview_widget()

        # 右侧设置区域
        settings_widget = self.create_settings_widget()

        # 设置区域比例
        body_layout.addWidget(file_list_widget, 2)
        body_layout.addWidget(preview_widget, 4)
        body_layout.addWidget(settings_widget, 2)

        self.main_layout.addLayout(body_layout, 1)

    def create_file_list_widget(self):
        # 文件列表区域
        file_group = QGroupBox("文件列表")
        file_layout = QVBoxLayout()
        file_layout.setSpacing(10)

        # 列表控件
        self.file_list = QListWidget()
        self.file_list.setSelectionMode(QListWidget.ExtendedSelection)

        # 添加示例文件
        files = [
            ("24、架构...践.pdf", True),
        ]
        for file_name, checked in files:
            item = QListWidgetItem()
            widget = QWidget()
            layout = QHBoxLayout(widget)
            layout.setContentsMargins(5, 5, 5, 5)

            checkbox = QCheckBox()
            checkbox.setChecked(checked)
            layout.addWidget(checkbox)

            label = QLabel(file_name)
            layout.addWidget(label)

            layout.addStretch(1)

            item.setSizeHint(widget.sizeHint())
            self.file_list.addItem(item)
            self.file_list.setItemWidget(item, widget)

        # 全选按钮
        self.select_all_label = QLabel("全选 (1/1)")

        # 按钮组
        button_layout = QHBoxLayout()
        button_layout.setSpacing(10)

        add_button = QPushButton("添加文件")
        clear_button = QPushButton("清空列表")

        button_layout.addWidget(add_button)
        button_layout.addWidget(clear_button)

        # 添加到文件布局
        file_layout.addWidget(self.file_list)
        file_layout.addWidget(self.select_all_label)
        file_layout.addLayout(button_layout)

        file_group.setLayout(file_layout)
        return file_group

    def create_preview_widget(self):
        # 预览区域
        preview_group = QGroupBox("输出效果预览")
        preview_layout = QVBoxLayout()
        preview_layout.setSpacing(15)

        # 预览框架
        preview_frame = QFrame()
        preview_frame.setMinimumSize(400, 300)
        preview_frame.setFrameShape(QFrame.StyledPanel)

        # 预览内容
        preview_content = QVBoxLayout(preview_frame)

        # 示例预览图片（实际应用中会加载实际图片）
        self.preview_label = QLabel("WebRTC系统构建")
        self.preview_label.setAlignment(Qt.AlignCenter)
        preview_font = QFont()
        preview_font.setPointSize(24)
        preview_font.setBold(True)
        self.preview_label.setFont(preview_font)
        self.preview_label.setStyleSheet("color: #3498DB;")

        preview_content.addWidget(self.preview_label)

        # 水印编辑按钮
        watermark_button = QPushButton("编辑水印 VIP")
        watermark_button.setEnabled(False)

        preview_layout.addWidget(preview_frame)
        preview_layout.addWidget(watermark_button, alignment=Qt.AlignCenter)

        preview_group.setLayout(preview_layout)
        return preview_group

    def create_settings_widget(self):
        # 设置区域
        settings_group = QGroupBox("输出设置")
        settings_layout = QVBoxLayout()
        settings_layout.setSpacing(15)

        # 网格布局 - 输出设置
        grid = QGridLayout()
        grid.setVerticalSpacing(12)
        grid.setHorizontalSpacing(15)

        # 输出方式
        grid.addWidget(QLabel("输出方式："), 0, 0)
        output_method = QComboBox()
        output_method.addItems(["逐页输出", "合成长图"])
        output_method.setCurrentIndex(0)
        grid.addWidget(output_method, 0, 1)

        # 水印设置
        grid.addWidget(QLabel("水印设置："), 1, 0)
        watermark_group = QWidget()
        watermark_layout = QHBoxLayout(watermark_group)
        watermark_layout.setSpacing(10)
        watermark_layout.setContentsMargins(0, 0, 0, 0)

        watermark_types = [
            ("无水印", True),
            ("VIP", False),
            ("默认水印", True),
            ("自定义水印 VIP", False),
        ]
        for text, enabled in watermark_types:
            btn = QPushButton(text)
            btn.setEnabled(enabled)
            btn.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
            watermark_layout.addWidget(btn)

        grid.addWidget(watermark_group, 1, 1)

        # 输出范围
        grid.addWidget(QLabel("输出范围："), 2, 0)
        output_range = QWidget()
        output_range_layout = QHBoxLayout(output_range)
        output_range_layout.setSpacing(10)
        output_range_layout.setContentsMargins(0, 0, 0, 0)

        range_btns = [
            ("所有页", True),
            ("页码选择 VIP", False),
        ]
        for text, enabled in range_btns:
            btn = QPushButton(text)
            btn.setEnabled(enabled)
            output_range_layout.addWidget(btn)

        grid.addWidget(output_range, 2, 1)

        # 输出格式
        grid.addWidget(QLabel("输出格式："), 3, 0)
        output_format = QComboBox()
        output_format.addItems(["PNG", "JPG", "BMP", "GIF", "TIFF"])
        grid.addWidget(output_format, 3, 1)

        # 输出品质
        grid.addWidget(QLabel("输出品质："), 4, 0)
        quality_group = QWidget()
        quality_layout = QHBoxLayout(quality_group)
        quality_layout.setSpacing(10)
        quality_layout.setContentsMargins(0, 0, 0, 0)

        quality_options = [
            ("标清(默认尺寸)", True),
            ("标清(默认尺寸2倍) VIP", False),
        ]
        for text, enabled in quality_options:
            btn = QPushButton(text)
            btn.setEnabled(enabled)
            quality_layout.addWidget(btn)

        grid.addWidget(quality_group, 4, 1)

        # 输出颜色
        grid.addWidget(QLabel("输出颜色："), 5, 0)
        color_group = QWidget()
        color_layout = QHBoxLayout(color_group)
        color_layout.setSpacing(10)
        color_layout.setContentsMargins(0, 0, 0, 0)

        color_options = [
            ("彩色", True),
            ("灰度", True),
            ("黑白", True),
        ]
        for text, enabled in color_options:
            btn = QPushButton(text)
            btn.setCheckable(True)
            btn.setEnabled(enabled)
            btn.setAutoExclusive(True)
            color_layout.addWidget(btn)
            if text == "彩色":
                btn.setChecked(True)

        grid.addWidget(color_group, 5, 1)

        # 添加到设置布局
        settings_layout.addLayout(grid)

        # 输出目录
        settings_layout.addWidget(QLabel("输出目录："))

        # 目录选项按钮
        dir_options_group = QWidget()
        dir_layout = QVBoxLayout(dir_options_group)
        dir_layout.setSpacing(5)

        dir_options = [
            ("原文件位置", "/Users/c...4_计算机网络/"),
            ("各文件生成对应文件夹", "")
        ]
        for text, path in dir_options:
            widget = QWidget()
            h_layout = QHBoxLayout(widget)
            h_layout.setContentsMargins(0, 0, 0, 0)

            radio = QCheckBox(text)
            if path:
                path_label = QLabel(path)
                path_label.setStyleSheet("color: #888;")
                h_layout.addWidget(radio)
                h_layout.addWidget(path_label)
                h_layout.addStretch(1)
            else:
                h_layout.addWidget(radio)

            if text == "原文件位置":
                radio.setChecked(True)

            dir_layout.addWidget(widget)

        settings_layout.addWidget(dir_options_group)

        settings_group.setLayout(settings_layout)
        return settings_group

    def create_bottom_buttons(self):
        # 底部按钮区域
        bottom_layout = QHBoxLayout()
        bottom_layout.setSpacing(10)

        close_button = QPushButton("关闭")
        close_button.setProperty("class", "close-button")
        # 开始输出按钮
        start_button = QPushButton("开始输出")
        start_button.setProperty("class", "start-button")

        bottom_layout.addStretch(1)
        bottom_layout.addWidget(start_button)
        bottom_layout.addWidget(close_button)

        self.main_layout.addLayout(bottom_layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FileProcessingUI()
    window.show()
    sys.exit(app.exec())
